<template>
	<view class="avatar" :style="{paddingTop: paddingHeight + 'px'}">
		<nav-bar title="头像"></nav-bar>
		<view class="container" @click="chooseMedia">
			<view class="text">
				头像
			</view>
			<view class="r-c">
				<u-image width="88rpx" height="88rpx" shape="circle" :showMenuByLongpress="false"
					:src="avatar ? avatar : 'http://oss.antaimedical.com/images/static/2024/06/666a5d1ceb14d0f049f44d0bq2D7khAF169T8422df8d4f59a27d947ae9a760e5895d.png'"
					:lazy-load="true"></u-image>
				<text class="iconfont icon-arrow_rigth"></text>
			</view>
		</view>
		<view class="save_h large_button" @click="$u.throttle(save, 1000)">保存</view>
		<u-modal :show="showMaxTips" @close="showMaxTips = false" @confirm="showMaxTips = false" confirmText="确定"
			confirmColor="#545DDE">
			<view class="tips">
				图片过大,请上传不大于10M的图片
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		uploadAvatar
	} from '@/api/index.js'
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				paddingHeight: uni.getStorageSync('navbarHeight'),
				userId: uni.getStorageSync('userId'),
				showMaxTips: false,
				avatar: ''
			};
		},
		computed: {
			...mapState({
				ble: state => state.ble, // 实时蓝牙全局状态
				userInfo: state => state.userInfo
			})
		},
		onLoad() {
			this.avatar = this.userInfo.avatar
		},
		methods: {
			// 打开相册选择图片
			chooseMedia() {
				getApp().globalData.isPriviewApi = true
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album'],
					sizeType: ['compressed'],
					success: (res) => {
						console.log('res', res)
						if (res.tempFiles[0].size >= 10485760) {
							this.showMaxTips = true
						} else {
							const src = res.tempFiles[0].tempFilePath;
							this.upload(src)
						}
					},
					fail: err => {
						console.log(err);
					}
				})
			},
			upload(src) {
				uni.showLoading({
					title: '上传中...',
					mask: true
				})
				// uploadStaticImg uploadImages
				uni.uploadFile({
					url: 'https://antaimedical.com:5000/devicewx-upload/api/v1/upload/AliyunOss/uploadImages',
					header: {
						Authorization: `Bearer ${uni.getStorageSync('token')}`
					},
					filePath: src,
					name: 'file',
					success: async (res) => {
						console.log('name', res)
						let obj = JSON.parse(res.data);
						if (obj.code == 200) {
							uni.showToast({
								title: '上传完成'
							})
							this.avatar = obj.data
						} else {
							uni.showToast({
								title: obj.code,
								icon: 'error',
								duration: 2000
							});
						}
					},
					complete: () => {
						uni.hideLoading();
					}
				});
			},
			async save() {
				let params = {
					// userId: this.userId,
					id: this.userInfo.bodyId,
					avatar: this.avatar
				}
				try {
					const res = await uploadAvatar(params)
					if (res.code == 200) {
						uni.showToast({
							title: '操作成功'
						})
						setTimeout(() => {
							uni.$emit('refreshUser')
							uni.navigateBack()
						}, 1800)
					}
				} catch (e) {
					//TODO handle the exception
				}
			}
		}
	};
</script>
<style lang="scss">
	page {
		background-color: #FAFAFA;
		min-height: 100%;
	}
</style>
<style lang="scss" scoped>
	.avatar {
		padding: 0 48rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.container {
			width: 100%;
			height: 118rpx;
			margin-top: 48rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 0 48rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.text {
				font-size: 32rpx;
				color: #666666;
			}

			.r-c {
				display: flex;
				align-items: center;

				.icon-arrow_rigth {
					color: rgba(#666666, .5);
					padding-left: 20rpx;
				}
			}
		}
	}

	.tips {
		padding: 14rpx 0 66rpx;
		font-weight: 500;
		font-size: 32rpx;
		color: #353535;
		font-family: PingFangSC-Medium, PingFang SC;
	}


	.save_h {
		position: fixed;
		bottom: 144rpx;
		left: 50%;
		transform: translateX(-50%);
	}
</style>